/*
 *    Copyright 2010 The maven-migrate-plugin Team
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */
package com.googlecode.mvnmigrate;

import java.io.File;
import java.io.PrintStream;
import java.lang.reflect.Constructor;

import org.apache.ibatis.migration.commands.BaseCommand;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;

import com.googlecode.mvnmigrate.util.MavenOutputStream;

/**
 * Provides to an abstract class that extends {@link AbstractMojo}.
 *
 * @version $Id: AbstractCommandMojo.java 132 2010-07-10 17:23:54Z marco.speranza79 $
 */
abstract public class AbstractCommandMojo extends AbstractMojo {

    /**
     * Location of migrate repository.
     *
     * @parameter expression="${repository}"
     * @required
     */
    protected File repository;

    /**
     * Environment to configure. Default environment is 'development'.
     *
     * @parameter expression="${environment}" default-value="development"
     */
    protected String environment;

    /**
     * Forces script to continue even if SQL errors are encountered.
     *
     * @parameter  expression="${force}" default-value="false"
     */
    protected boolean force;

    /**
     * Skip migration actions.
     *
     * @parameter  expression="${migration.skip}" default-value="false"
     */
    protected boolean skip;

    /**
     * The command to execute.
     */
    private BaseCommand command;

    /**
     * The command class to instanziate.
     */
    private final Class<? extends BaseCommand> commandClass;

    /**
     * Constructor.
     *
     * @param commandClass Command {@link Class} to be istanziate.
     */
    public AbstractCommandMojo(final Class<? extends BaseCommand> commandClass) {
        super();
        this.commandClass = commandClass;

    }

    /**
     * execute the command.
     */
    public void execute() throws MojoExecutionException, MojoFailureException {
        if (isSkip()) return;
        init();
        command.execute();
    }

    /**
     * Initialize the ibatis command.
     */
    protected void init() throws MojoFailureException  {

        try {
            final Constructor<? extends BaseCommand> c = commandClass.getConstructor(File.class, String.class, boolean.class);
            this.command = c.newInstance(this.repository, this.environment, this.force);

            final PrintStream out = new PrintStream(new MavenOutputStream(getLog()));
            this.command.setPrintStream(out);
            this.command.setDriverClassLoader(getClass().getClassLoader());
            getLog().info(
                    "Executing  Apache Migration "
                        + this.command.getClass().getSimpleName());

        } catch (Exception e) {
            throw new MojoFailureException(this, e.getMessage(), e
                    .getLocalizedMessage());
        }
    }

    /**
     * Return the command.
     * @return {@link BaseCommand} the command created.
     */
    protected BaseCommand getCommand() {
        return this.command;
    }

    /**
     * Test if the skip flag is setted.
     * @return the skip flag.
     */
    protected boolean isSkip() {
        if (this.skip && this.getLog().isDebugEnabled()) {
            this.getLog().info("Apache Migration command "
                    + this.command.getClass().getSimpleName()
                    + " skipped!");
        }
        return skip;
    }

}
